package com.yangzb.spring.batch.service;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Service;
import top.chaser.framework.common.base.util.JSONUtil;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author : dylanz
 * @since : 08/25/2020
 */
@Service
@Slf4j
public class ItemWriterService implements ItemWriter<Map> {
    public static AtomicInteger count = new AtomicInteger();
    //在此处进行数据输出操作，如写入数据库、写入文件、打印log等，本例为打印log；
    public void write(List<? extends Map> messages) throws Exception {
        log.info("write messages");
        for (Map message : messages) {
//            log.info("Writing data: " + JSONUtil.toJSONString(message));
            count.getAndIncrement();
            if(count.get()%10000==0) {
                log.info("write {},{}",count.get(),messages.size());
            }
        }
    }
}
